package cn.picclife.mwx.manager.sys.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import cn.picclife.mwx.manager.sys.model.bo.CheckQualityRequest;
import cn.picclife.mwx.manager.sys.model.bo.CheckQualityRes;
import cn.picclife.mwx.manager.sys.model.bo.DataResponse;
import cn.picclife.mwx.manager.sys.model.bo.ObjectResponse;
import cn.picclife.mwx.manager.sys.model.bo.PageCount;
import cn.picclife.mwx.manager.sys.model.bo.PageResponse;
import cn.picclife.mwx.manager.sys.model.po.Branch;
import cn.picclife.mwx.manager.sys.service.CheckQualityService;
import cn.picclife.mwx.manager.sys.service.impl.CheckQualityServiceImpl;
import cn.picclife.mwx.manager.util.enums.ResponseStatusEnum;

@RequestMapping("check")
@RestController
public class CheckQualityController {

	private Log log = LogFactory.getLog(CheckQualityController.class);
	
	@Autowired
	private CheckQualityService checkQualityServiceImpl;
	
	/**
	 * 获取抽查列表
	 * @param req
	 * @return
	 */
	@RequestMapping(value="/getDataCount", method= {RequestMethod.POST})
	public PageResponse getDataCount(@RequestBody CheckQualityRequest req) {
		
		log.info("===========getDataCount获取质检总数据量接口请求报文："+JSONObject.toJSON(req).toString());
		PageResponse pageResponse = new PageResponse();
		/*if(null==req) {
			dataResponse.setCode(ResponseStatusEnum.PARAM_IS_NULL.getCode());
			dataResponse.setMsg(ResponseStatusEnum.PARAM_IS_NULL.getName());
			log.info("===========distributePerson质检分配人员接口返回报文："+JSONObject.toJSON(dataResponse).toString());
			return dataResponse;
		}*/
		try {
			PageCount pageCount = checkQualityServiceImpl.getDistributeCount(req);
			pageResponse.setPageCount(pageCount);
		} catch (Exception e) {
			log.error(e);
			pageResponse.setCode(ResponseStatusEnum.EXCEPTION.getCode());
			pageResponse.setMsg(ResponseStatusEnum.EXCEPTION.getName());
			log.info("===========getDataCount获取质检总数据量接口返回报文："+JSONObject.toJSON(pageResponse).toString());
			return pageResponse;
		}
		pageResponse.setCode(ResponseStatusEnum.SUCCESS_CODE.getCode());
		pageResponse.setMsg(ResponseStatusEnum.SUCCESS_CODE.getName());
		log.info("===========getDataCount获取质检总数据量接口返回报文："+JSONObject.toJSON(pageResponse).toString());
		return pageResponse;
	}
	
	/**
	 * 获取抽查数据
	 * @param req
	 * @return
	 */
	@RequestMapping(value="/distributePerson", method= {RequestMethod.POST})
	public DataResponse distributePerson(@RequestBody CheckQualityRequest req,HttpServletRequest request) {
		
		log.info("===========distributePerson获取百分比质检数据接口请求报文："+JSONObject.toJSON(req).toString());
		DataResponse dataResponse = new DataResponse();
		if(null==req||null==req.getPercent()||req.getPercent().trim().length()<=0) {
			dataResponse.setCode(ResponseStatusEnum.PARAM_IS_NULL.getCode());
			dataResponse.setMsg(ResponseStatusEnum.PARAM_IS_NULL.getName());
			log.info("===========distributePerson质检分配人员接口返回报文："+JSONObject.toJSON(dataResponse).toString());
			return dataResponse;
		}
		try {
			int num = checkQualityServiceImpl.getDistributeData(req);
			if(num==2) {
				dataResponse.setCode(ResponseStatusEnum.PERCENT_FAIL.getCode());
				dataResponse.setMsg(ResponseStatusEnum.PERCENT_FAIL.getName());
				log.info("===========distributePerson获取百分比质检数据接口返回报文："+JSONObject.toJSON(dataResponse).toString());
				return dataResponse;
			}
			if(num==3) {
				dataResponse.setCode(ResponseStatusEnum.CHECK_NO_DATA.getCode());
				dataResponse.setMsg(ResponseStatusEnum.CHECK_NO_DATA.getName());
				log.info("===========distributePerson获取百分比质检数据接口返回报文："+JSONObject.toJSON(dataResponse).toString());
				return dataResponse;
			}
			if(num==0) {
				dataResponse.setCode(ResponseStatusEnum.FAIL_CODE.getCode());
				dataResponse.setMsg(ResponseStatusEnum.FAIL_CODE.getName());
				log.info("===========distributePerson获取百分比质检数据接口返回报文："+JSONObject.toJSON(dataResponse).toString());
				return dataResponse;
			}
		} catch (Exception e) {
			log.error(e);
			dataResponse.setCode(ResponseStatusEnum.EXCEPTION.getCode());
			dataResponse.setMsg(ResponseStatusEnum.EXCEPTION.getName());
			log.info("===========distributePerson获取百分比质检数据接口返回报文："+JSONObject.toJSON(dataResponse).toString());
			return dataResponse;
		}
		dataResponse.setCode(ResponseStatusEnum.SUCCESS_CODE.getCode());
		dataResponse.setMsg(ResponseStatusEnum.SUCCESS_CODE.getName());
		log.info("===========distributePerson获取百分比质检数据接口返回报文："+JSONObject.toJSON(dataResponse).toString());
		return dataResponse;
	}
	
	/**
	 * 获取机构数
	 * @param req
	 */
	@RequestMapping(value="/getBranchList", method= {RequestMethod.POST})
	public DataResponse getBranchList() {
		
		log.info("===========getBranchList获取机构接口调用开始");
		DataResponse pageResponse = new DataResponse();
		try {
			List<Branch>list = checkQualityServiceImpl.getBranchList();
			pageResponse.setData(list);
		} catch (Exception e) {
			log.error(e);
			pageResponse.setCode(ResponseStatusEnum.EXCEPTION.getCode());
			pageResponse.setMsg(ResponseStatusEnum.EXCEPTION.getName());
			log.info("===========getBranchList获取机构接口返回报文："+JSONObject.toJSON(pageResponse).toString());
			return pageResponse;
		}
		pageResponse.setCode(ResponseStatusEnum.SUCCESS_CODE.getCode());
		pageResponse.setMsg(ResponseStatusEnum.SUCCESS_CODE.getName());
		log.info("===========getBranchList获取机构接口返回成功！===========");
		return pageResponse;
	}
}
